package com.jt;

import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class TestMybatis {
    @Test
    public void demo1() throws IOException {
        // 指定配置文件的目录
        String resource = "mybatis-config.xml";
        // 通过IO流 加载配置文件  org.apache.ibatis.io;包路径
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        // 实例化工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        // 开启SqlSession 连接 通过sqlSession 可以直接操作数据库
        SqlSession sqlSession = build.openSession();
        // 获取接口对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        // 调用接口方法
        List<User> all = mapper.findAll();
        System.out.println(all);
        // 手动关闭
        sqlSession.close();
    }
}
